package net.gyapp.markim;

import android.content.Context;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;

/**
 * Auther:  winds
 * Data:    2017/11/6
 * Version: 1.0
 * Desc:
 */

@GlideModule
public class GlideModuleSetting extends AppGlideModule {
    // 默认存放图片的路径
    public final static String DEFAULT_SAVE_IMAGE_PATH = AppConfig.DEFAULT_CACHE_PATH;

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        super.applyOptions(context, builder);
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, AppConfig.DISK_CACHE_SIZE));
        //设置磁盘缓存的路径 path
        final File cacheDir = new File(DEFAULT_SAVE_IMAGE_PATH);
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }

        builder.setDiskCache(new DiskCache.Factory() {
            @Override
            public DiskCache build() {
                return DiskLruCacheWrapper.get(cacheDir, AppConfig.DISK_CACHE_SIZE);
            }
        });
        //设置内存缓存大小，一般默认使用glide内部的默认值
        builder.setMemoryCache(new LruResourceCache(AppConfig.MAX_MEMORY_CACHE_SIZE));
        builder.setDefaultRequestOptions(new RequestOptions().format(DecodeFormat.PREFER_ARGB_8888));
    }
}
